<?php
namespace app\agent\controller\help;

use app\agent\controller\Controller;
use app\agent\model\agent\AgentWxapp as AgentWxappModel;
use think\Session;
use think\Db;

class Help extends Controller{
    /**
     * 小程序配置首页
     */
    public function index(){
        $session = Session::get('yoshop_agent');
        $agent_id = $session['agent']['agent_id'];
        $model = AgentWxappModel::getInfo($agent_id);
        $info=Db::name('agent_help')->where(['agent_wxapp_id'=>$model->agent_wxapp_id])->find();
        if (!$this->request->isAjax()) {
            return $this->fetch('index', ['model'=>$info]);
        }
        if(empty($info)){
            //更新帮助中心设置
            $data['content']=input('post.content');
            $data['title']=input('post.title');
            $data['agent_wxapp_id']=$model->agent_wxapp_id;
            $data['create_time']=time();
            $addStatus=Db::name('agent_help')->insert($data);
            if($addStatus){
                return $this->renderSuccess('更新成功');
            }
        }else{
            //更新小程序设置
            $data['content']=input('post.content');
            $data['title']=input('post.title');
            $upStatus=Db::name('agent_help')->where(['agent_wxapp_id'=>$model->agent_wxapp_id])->update($data);
            if($upStatus){
                return $this->renderSuccess('更新成功');
            }
        }
        return $this->renderError( '更新失败');
    }
}